<template>
  <view>
    <!-- 1. 搜索栏 -->
    <my-search @click="gotoSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧一级分类 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height:wh + 'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i == active ? 'active':'']" @click="navClickHandler(i)">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧的二、三级分类 -->
      <scroll-view class="right-scroll-view" scroll-y :style="{height:wh + 'px'}">
        <!-- 二级分类展示 -->
        <view class="cate_lev2" v-for="(item2,i2) in lev2List" :key="i2">
          / {{item2.cat_name}} /
          <!-- 三级分类展示 -->
          <view class="cate_lev3">
            <view class="cate_lev3_item"  v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
              <!-- 上部分的图片 -->
             
                <image :src="item3.cat_icon" ></image>
             
              <!-- 下部分的文字 -->
              <text>
                {{item3.cat_name}}
              </text>
            </view>
          
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
   import mixInBadge from "@/mixins/tabbar-badge.js"
  export default {
    mixins:[mixInBadge],
    data() {
      return {
        // 分类列表
        cateList: [],
        // 窗口的实际高度
        wh: 0,
        active: 0,
        // 二级分类列表
        lev2List: [],
      };
    },
    onLoad() {
      // 1. 获取所有的分类列表
      this.getCateList()
      // 2. 获取实际窗口高度
      const systemInfo = uni.getSystemInfoSync()
      this.wh = systemInfo.windowHeight - 50
    },
    methods: {
      // 1. 获取所有的分类列表
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/categories")
        console.log("res:", res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message

        this.lev2List = this.cateList[0].children
      },
      // 点击某个一级分类时得到二级分类
      navClickHandler(i) {
        this.active = i
        // 得到二级分类列表
        this.lev2List = this.cateList[i].children
      },
      // 点击某个三级分类,跳转到商品列表(展示这个分类下的所有商品列表)
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch(){
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
      },
    },
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      background-color: #f7f7f7;
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        font-size: 12px;
        text-align: center;

        &.active {
          background-color: #fff;
          position: relative;

          // 渲染激活项左侧的红色指示边线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }

      }
    }

    .right-scroll-view {
      background-color: #fff;
      text-align: center;

      .cate_lev2 {
        color: black;
        padding: 10px 0;
        .cate_lev3{
          display: flex;
          flex-wrap: wrap;
           .cate_lev3_item{
              width: 33.3%;
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: center;
              margin: 10px 0;
               image{
                 width: 60px;
                 height: 60px;
               }
          
             text{
               font-size: 12px;
             }
           }
       
        }
      }
    }
  }
</style>